home *** CD-ROM | disk | FTP | other *** search
- #include "FMAT.h"
-
-
- /* GetButtonStr: Retrieve string from NumberParts for the requested token's button */
- static void GetButtonStr( NumberParts *parts, short tok, StringPtr s )
- {
- WideChar *wc;
- StringPtr p;
-
- short numChars;
- short i, len;
-
-
- switch( tok ) {
-
- default:
- numChars = 1;
- wc = &parts->data[ tok - 1 ];
- break;
-
- case tokPEPlus:
- numChars = parts->pePlus.size + 1;
- wc = parts->pePlus.data;
- break;
-
- case tokPEMinus:
- numChars = parts->peMinus.size + 1;
- wc = parts->peMinus.data;
- break;
-
- case tokPEMinusPlus:
- numChars = parts->peMinusPlus.size + 1;
- wc = parts->peMinusPlus.data;
- break;
- }
-
- /* Copy wide characters */
- p = s + 1;
- len = 0;
-
- for( i = 0; i < numChars; i++, wc++ ) {
-
- if( wc->a[ 0 ]) {
- *p++ = wc->a[ 0 ];
- len++;
- }
-
- if( wc->a[ 1 ]) {
- *p++ = wc->a[ 1 ];
- len++;
- }
- }
-
- s[ 0 ] = len;
- }
-
- /* NewButtonRow: Start a new row of buttons */
- static void NewButtonRow( Point *where, short lineHeight )
- {
- where->h = 2;
- where->v += lineHeight + 8;
- }
-
- /* MakeButtonRect: Size and position the rectangle needed to hold this button's name */
- static void MakeButtonRect( NumberPartsPaletteHandle thePalette, NumberParts *parts,
- short tok, Point *where, short lineHeight, short widMax )
- {
- Rect r;
- Str31 token;
-
- short tokWidth;
-
-
- GetButtonStr( parts, tok, token );
- tokWidth = StringWidth( token );
- if( tokWidth < widMax )
- tokWidth = widMax;
-
- SetRect( &r, 0, 0, tokWidth + 6, lineHeight + 6 );
-
- if( where->h + r.right > ( *thePalette )->fw.wind->portRect.right - 2 )
- NewButtonRow( where, lineHeight );
-
- OffsetRect( &r, where->h, where->v );
- where->h = r.right + 2;
-
- ( *thePalette )->buttonList[ tok - 1 ] = r;
- }
-
- /* NewNumberPartsPalette: Create a new number parts palette */
- NumberPartsPaletteHandle NewNumberPartsPalette( rFMATHandle myFMAT )
- {
- Point where;
-
- WindowPtr theWindow;
- NumberPartsPaletteHandle thePalette;
-
-
- /* Allocate storage */
- thePalette = ( NumberPartsPaletteHandle )NewHandle( sizeof( NumberPartsPalette ));
- if( !thePalette )
- return 0;
-
- /* Get parent (dialog) window to find palette's default position */
- theWindow = ( WindowPtr )( *myFMAT )->w.wind;
- SetPort( theWindow );
-
- /* Position beside parent */
- SetPt( &where, theWindow->portRect.right + 5, theWindow->portRect.top );
- LocalToGlobal( &where );
-
- /* Make a new floating window */
- theWindow = FloatingWindowSetup( ResID( 1 ), ( FloatingWindowHandle )thePalette,
- ( ParentHandle )myFMAT, where );
- if( !theWindow ) {
- DisposHandle(( Handle )thePalette );
- return 0;
- }
-
- return thePalette;
- }
-
- /* InitNumberPartsPalette: Position all buttons and text on the palette */
- void InitNumberPartsPalette( NumberPartsPaletteHandle thePalette, NumberParts *parts,
- short parentFont )
- {
- Point where, loc;
- Rect empty;
- FontInfo info;
- Str255 s;
-
- WindowPtr theWindow;
- long result;
-
- short lineHeight;
- short tok;
-
-
- theWindow = ( *thePalette )->fw.wind;
- SetPort( theWindow );
-
- /* Determine the font to use (small fond of parent's font's script) */
- result = GetScript( Font2Script( parentFont ), smScriptSmallFondSize );
- TextFont( HiWord( result ));
- TextSize( LoWord( result ));
- GetFontInfo( &info );
- lineHeight = info.ascent + info.descent + info.leading;
-
- /* Initialize the button rectangles */
- SetPt( &where, 2, 2 );
- SetRect( &empty, 0, 0, 0, 0 );
-
- /* Left & right quotes on either side of the descriptive text */
- MakeButtonRect( thePalette, parts, tokLeftQuote, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kLiteralStr - 1 ] = loc;
- GetIndString( s, ResID( kNumberPartsStringID ), 1 );
- where.h += StringWidth( s );
- MakeButtonRect( thePalette, parts, tokRightQuote, &where, lineHeight, info.widMax );
- NewButtonRow( &where, lineHeight );
-
- /* Plus & minus buttons */
- MakeButtonRect( thePalette, parts, tokPlusSign, &where, lineHeight, info.widMax );
- MakeButtonRect( thePalette, parts, tokMinusSign, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kSignsStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Digit buttons */
- MakeButtonRect( thePalette, parts, tokLeadPlacer, &where, lineHeight, info.widMax );
- MakeButtonRect( thePalette, parts, tokZeroLead, &where, lineHeight, info.widMax );
- MakeButtonRect( thePalette, parts, tokNonLeader, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kDigitsStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Thousands button */
- MakeButtonRect( thePalette, parts, tokThousands, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kThousandsStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Decimal point button */
- MakeButtonRect( thePalette, parts, tokDecPoint, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kDecPointStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Percentage symbol button */
- MakeButtonRect( thePalette, parts, tokPercent, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kPercentStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Escape button */
- MakeButtonRect( thePalette, parts, tokEscape, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kEscapeStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Separator button */
- MakeButtonRect( thePalette, parts, tokSeparator, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kSeparatorStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* These guys aren't used */
- ( *thePalette )->buttonList[ tokLeader - 1 ] = empty;
- ( *thePalette )->buttonList[ 11 - 1 ] = empty;
- ( *thePalette )->buttonList[ tokPEMinusPlus - 1 ] = empty;
-
- /* E+ and E- buttons */
- MakeButtonRect( thePalette, parts, tokPEPlus, &where, lineHeight, info.widMax );
- MakeButtonRect( thePalette, parts, tokPEMinus, &where, lineHeight, info.widMax );
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kExponentsStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Unquoted literals label */
- SetPt( &loc, where.h, where.v + info.ascent + 3 );
- ( *thePalette )->staticTextList[ kUnquotedStr - 1 ] = loc;
- NewButtonRow( &where, lineHeight );
-
- /* Unquoted literals buttons */
- for( tok = /*tokUnquoteds*/ 15; tok <= tokMaxSymbols; tok++ ) {
- if( parts->data[ tok - 1 ].b )
- MakeButtonRect( thePalette, parts, tok, &where, lineHeight, info.widMax );
- else
- ( *thePalette )->buttonList[ tok - 1 ] = empty;
- }
- NewButtonRow( &where, lineHeight );
-
- /* Resize the palette to accommodate all buttons */
- SizeWindow( theWindow, theWindow->portRect.right, where.v, FALSE );
- InvalRect( &theWindow->portRect );
- }
-
- /* DrawTokenButton: Draw a token's button */
- static void DrawTokenButton( NumberPartsPaletteHandle thePalette, short tok,
- NumberParts *parts, WindowPtr theWindow, FontInfo *info )
- {
- Rect r;
- Str31 token;
-
- short center;
-
-
- /* No need if not in visRgn */
- r = ( *thePalette )->buttonList[ tok-1 ];
- if( !RectInRgn( &r, theWindow->visRgn ))
- return;
-
- /* Retrieve its string and center it */
- GetButtonStr( parts, tok, token );
- center = info->widMax - StringWidth( token );
- if( center < 0 )
- center = 0;
- else
- center >>= 1;
-
- /* Draw the button */
- EraseRect( &r );
- FrameRoundRect( &r, 6, 6 );
- MoveTo( r.left + 3 + center, r.top + 3 + info->ascent );
- DrawString( token );
- }
-
- /* UpdatePalette: Handle update event for number parts palette */
- void UpdatePalette( NumberPartsPaletteHandle thePalette )
- {
- Point loc;
- FontInfo info;
- NumberParts parts;
- Str255 s;
-
- WindowPtr theWindow;
-
- short tok, i, id;
-
-
- theWindow = ( *thePalette )->fw.wind;
- SetPort( theWindow );
- EraseRgn( theWindow->visRgn );
- GetFontInfo( &info );
- parts = ( *( rFMATHandle )( *thePalette )->fw.father )->partsTable;
-
- /* Draw all buttons */
- for( tok = tokLeftQuote; tok < kNumPaletteButtons; tok++ ) {
- DrawTokenButton( thePalette, tok, &parts, theWindow, &info );
- }
-
- /* Draw all text labels */
- id = ResID( kNumberPartsStringID );
- for( i = kLiteralStr; i < kNumPaletteStaticText; i++ ) {
- GetIndString( s, id, i );
- loc = ( *thePalette )->staticTextList[ i - 1 ];
- MoveTo( loc.h, loc.v );
- DrawString( s );
- }
- }
-
- /* ClickPaletteButton: Perform button operation */
- static void ClickPaletteButton( NumberPartsPaletteHandle thePalette, short tok )
- {
- Str31 token;
-
- rFMATHandle myFMAT;
- TEHandle hTE;
-
-
- myFMAT = ( rFMATHandle )( *thePalette )->fw.father;
- GetButtonStr( &( *myFMAT )->partsTable, tok, token );
- hTE = (( DialogPeek )( *thePalette )->fw.ownerWindow )->textH;
-
- /* Delete selected text */
- TEDelete( hTE );
-
- /* Insert token */
- TEInsert( token + 1, Length( token ), hTE );
-
- /* Update the examples */
- UpdateExamples( myFMAT );
- }
-
- /* TrackRoundRect: Track mouse & provide visual feedback */
- static Boolean TrackRoundRect( Point where, Rect *r, short ovalWidth, short ovalHeight )
- {
- Boolean inside;
- Point oldPt, newPt;
-
-
- /* Assume we start inside the rect */
- inside = TRUE;
- InvertRoundRect( r, ovalWidth, ovalHeight );
- oldPt = where;
-
- /* Wait for button to be released */
- while( StillDown()) {
-
- /* Get new mouse location and see if it's changed */
- GetMouse( &newPt );
- if( !DeltaPoint( oldPt, newPt ))
- continue;
-
- /* Mouse has moved; use new location */
- oldPt = newPt;
-
- /* See if status has changed */
- if( PtInRect( oldPt, r ) == inside )
- continue;
-
- /* Status has changed; invert the rect and switch status */
- InvertRoundRect( r, ovalWidth, ovalHeight );
- inside = !inside;
- }
-
- /* If still inside after button release, turn the rect back off */
- if( inside )
- InvertRoundRect( r, ovalWidth, ovalHeight );
-
- /* Report success to caller */
- return inside;
- }
-
- /* ClickPalette: Handle a mouseDown event in the number parts palette */
- void ClickPalette( EventRecord *evt, NumberPartsPaletteHandle thePalette )
- {
- Point where;
- Rect r;
-
- WindowPtr theWindow;
-
- short tok;
-
-
- theWindow = ( *thePalette )->fw.wind;
- SetPort( theWindow );
-
- where = evt->where;
- GlobalToLocal( &where );
-
- /* Find which button was clicked */
- for( tok = tokLeftQuote; tok <= tokMaxSymbols + 3; tok++ ) {
- r = ( *thePalette )->buttonList[ tok-1 ];
- if( PtInRect( where, &r )) {
-
- /* Found it! Track mouse and perform action */
- if( TrackRoundRect( where, &r, 6, 6 ))
- ClickPaletteButton( thePalette, tok );
- break;
- }
- }
- }
-
- /* CloseNumberPartsPalette: Free storage */
- void CloseNumberPartsPalette( NumberPartsPaletteHandle thePalette )
- {
- WindowPtr theWindow;
-
-
- theWindow = ( *thePalette )->fw.wind;
- CloseWindow( theWindow );
- WindReturn( theWindow );
-
- DisposHandle(( Handle )thePalette );
- }
-